<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller{


	public function goodsnew(){
		
	}

	/**
	* goods列表加分页
	*/
	public function goodslist(){

		if($_POST['cat_id']){
			$db = D('Goods');
			$data = $db -> create();
			$res = $db->where('cat_id=%d',$data['cat_id'])->select();
			$this->assign('list',$res);

			$count=$db->where('cat_id=%d',$data['cat_id'])->count();
			$Page = new \Think\Page($count,6);
			$Page->parameter['cat_id'] = urlencode($data['cat_id']);
			$show = $Page->show();
			$this->assign('page',$show);

			//种类的处理
			$cate2 = D('Cate');
			$value2 = $cate2->catelist();
			$this->assign('cate2',$value2);
			$this->display('goodslist');

		} else {
			$get1 = $_GET['p']?$_GET['p']:1;
			$user = D('Goods');
			$list = $user->page($get1.',6')->select();
			$this->assign('list',$list);
			$count = $user->count();
			$page = new \Think\Page($count,6);
			$page->setConfig('prev',"上一页");
			$page->setConfig('next',"下一页");
			$show1 = $page->show();
			$this->assign('page',$show1);
		    //种类的处理
			$cate2 = D('Cate');
			$value2 = $cate2->catelist();
			$this->assign('cate2',$value2);
			$this->display('goodslist');		
	    }
		
	}

	/**
	* 添加goods
	*/
	public function goodsadd(){
		$cate2 = D('Cate');
		$value2 = $cate2->catelist();
		$this->assign('cate2',$value2);
		if(IS_POST){
			$db = D('Goods');//连接数据库
			$data = $db->create(); //将后台提交过来的内容，直接保存到数据库，前提name和字段名一致
			$value1 = $this->upload();
			$data['goods_img'] = './Public/images/'.$value1["savepath"].$value1["savename"];
			$value2 = $this->thumb1($data['goods_img']);
			//var_dump($value2);exit;
			$data['goods_img'] = str_replace('./Public','/Public',$data['goods_img']);
			$data['thumb_img'] = str_replace('./Public','/Public',$value2);
			//$data['thumb_img'] = $value2;
			$data['add_time'] = time();
			$data['last_update'] = time();
			$db->add($data);
			$this->redirect('Goods/goodslist');
		} else {
			$this->display();
		}	
	}

    /**
    * 查看goods信息
    */
	public function goodscheck(){
			$id = $_GET['id'];
			$db = D('Goods');//连接数据库
			$res = $db->where("goods_id=%d",$id)->select();
			$cateModel = D('Cate');
			$value2 = $cateModel -> catelist();
			$this->assign('vovo',$value2);
			$this->assign('res',$res);
			$this->display();
	}


	/**
	* 修改goods的信息
	*/
	public function goodsmodify(){

		if(IS_GET && !IS_POST){
			$id = $_GET['id'];
			$db = D('Goods');//连接数据库
			$res = $db->where("goods_id=%d",$id)->select();
			$cateModel = D('Cate');
			$catevalue = $cateModel->catelist();
			$this->assign('catemodel',$catevalue);
			$this->assign('res',$res);
			$this->display();
		} else if(IS_POST){
			$id = I('get.id');
			$db = D('Goods');
			$data = $db->create();
			$data['last_update'] = time();
			$res = $db->where("goods_id=%d",$id)->save($data);
			$this->redirect('Goods/goodslist');//注意goodslist方法中一定要指定display的内容
		}	
	}
	/**
	* 删除goods信息
	*/
	public function goodsdel(){
		$id = $_GET['id'];
		$db = D('Goods');//连接数据库
		$db->where('goods_id=%d',$id)->delete();
		$res = $db->select();
		$this->assign('res',$res);
		$this->redirect('Goods/goodslist');
	}


	/**
	* 上传图片文件方法
	*/

	public function upload(){
		$upload = new \Think\Upload();
		$upload -> maxSize = 3145728;
		$upload -> exts = array('jpg','gif','png','jpeg');
		$upload -> rootPath = './Public/images/';
		$upload->autoSub = true;//开启子目录模式
		$upload->subName = date('Y-m-d').'/goods_img';
		$info = $upload -> uploadOne($_FILES['goods_img']); 
		if(!$info){
			return $this->error($upload->getError());
		} else {
			return $info;
		}
	}


	public function thumb1($path){
		$image = new \Think\Image();
		$image->open($path);
		$thumb_path = str_replace('goods_img','thumb_img',$path);
		$result1 = $image->thumb(150,150)->save($thumb_path);
		if($result1){
			return $thumb_path;
		} else {
			return "false";
		}
	}


}